'use strict';
const axios = require('axios');
const moment = require('moment');

const Subscription = require('egg').Subscription;
class wxchat extends Subscription {

    static get schedule() {
        return {
            interval: '3600s', // 任务的间隔时间, 这个定义的1小时执行一次
            type: 'worker', // 指定所有的 worker都需要执行
            immediate: true, // 项目启动该任务就开始执行
        };
      }

    async subscribe() {
        const token = await axios.get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx833268732e68920c&secret=92d5f8b18485998950486d25a41ca14c');
        console.log('token', token.data);
        this.app.mysql.update('wx_params', {
            name: 'access_token',
            content: token.data.access_token,
            update_time: moment().format('YYYYMMDDHHmmss'),
        }, {
            where: {
                id: 1,
            }
        });
    }
}

module.exports = wxchat;
